const axios = require('axios');

(async () => {
  console.log('🔄 最终验证修复后的勋章显示功能');
  
  try {
    // 1. 登录用户
    const loginResponse = await axios.post('http://localhost:3000/api/auth/login', {
      email: 'admin@campus.com',
      password: 'admin123'
    });
    
    const authToken = loginResponse.data.token;
    const userId = loginResponse.data.user.id;
    console.log(`✅ 用户登录成功: ID=${userId}`);
    
    // 2. 获取当前用户所有的已获得勋章和可获得勋章
    console.log('\n🏆 获取勋章数据...');
    
    // "全部勋章"
    const allResponse = await axios.get('http://localhost:3000/api/badges?limit=100', {
      headers: { Authorization: `Bearer ${authToken}` }
    });
    
    // "我的勋章"
    const ownedResponse = await axios.get('http://localhost:3000/api/badges?filter=owned&limit=100', {
      headers: { Authorization: `Bearer ${authToken}` }
    });
    
    // "可获得勋章"
    const availableResponse = await axios.get('http://localhost:3000/api/badges?filter=available&limit=100', {
      headers: { Authorization: `Bearer ${authToken}` }
    });
    
    // 3. 分析结果
    const ownedBadges = ownedResponse.data.badges;
    const availableBadges = availableResponse.data.badges;
    const allBadges = allResponse.data.badges;
    
    // 统计数量
    console.log('\n📊 勋章数量:');
    console.log(`已获得勋章(owned): ${ownedBadges.length}个`);
    console.log(`可获得勋章(available): ${availableBadges.length}个`);
    console.log(`全部勋章(all): ${allBadges.length}个`);
    
    // 预期: 全部勋章应该是已获得勋章和可获得勋章的总和
    const expectedTotal = ownedBadges.length + availableBadges.length;
    console.log(`预期全部勋章数量: ${expectedTotal}个`);
    
    // 检查重叠
    const ownedIds = ownedBadges.map(badge => badge.id);
    const availableIds = availableBadges.map(badge => badge.id);
    const allIds = allBadges.map(badge => badge.id);
    
    // 检查已获得勋章是否在全部勋章中
    const ownedInAllCount = ownedIds.filter(id => allIds.includes(id)).length;
    console.log(`已获得勋章在全部勋章中出现: ${ownedInAllCount}/${ownedBadges.length}`);
    
    // 检查可获得勋章是否在全部勋章中
    const availableInAllCount = availableIds.filter(id => allIds.includes(id)).length;
    console.log(`可获得勋章在全部勋章中出现: ${availableInAllCount}/${availableBadges.length}`);
    
    // 检查已获得勋章和可获得勋章是否有重叠
    const overlapCount = ownedIds.filter(id => availableIds.includes(id)).length;
    console.log(`已获得勋章和可获得勋章重叠: ${overlapCount}个`);
    
    // 检查已获得勋章标记
    const correctlyMarkedOwned = ownedBadges.filter(badge => badge.isOwned === true).length;
    console.log(`已获得勋章正确标记isOwned=true: ${correctlyMarkedOwned}/${ownedBadges.length}`);
    
    // 检查可获得勋章标记
    const correctlyMarkedAvailable = availableBadges.filter(badge => badge.isOwned === false).length;
    console.log(`可获得勋章正确标记isOwned=false: ${correctlyMarkedAvailable}/${availableBadges.length}`);
    
    // 4. 获取缺失的勋章
    const missingInAll = [
      ...ownedIds.filter(id => !allIds.includes(id)),
      ...availableIds.filter(id => !allIds.includes(id))
    ];
    
    if (missingInAll.length > 0) {
      console.log('\n❌ 在全部勋章中缺失的勋章ID:');
      console.log(missingInAll.join(', '));
      
      // 分析缺失的勋章
      console.log('\n📋 缺失勋章的详细信息:');
      
      // 检查缺失的已获得勋章
      const missingOwned = ownedIds.filter(id => !allIds.includes(id));
      if (missingOwned.length > 0) {
        console.log(`缺失的已获得勋章: ${missingOwned.length}个`);
        missingOwned.forEach(id => {
          const badge = ownedBadges.find(b => b.id === id);
          console.log(`  - ID: ${badge.id}, 名称: ${badge.badge_name || '未命名'}`);
        });
      }
      
      // 检查缺失的可获得勋章
      const missingAvailable = availableIds.filter(id => !allIds.includes(id));
      if (missingAvailable.length > 0) {
        console.log(`缺失的可获得勋章: ${missingAvailable.length}个`);
        missingAvailable.forEach(id => {
          const badge = availableBadges.find(b => b.id === id);
          console.log(`  - ID: ${badge.id}, 名称: ${badge.badge_name || '未命名'}`);
        });
      }
    }
    
    // 5. 调查潜在徽章(potential_)数量
    const potentialInAll = allBadges.filter(badge => String(badge.id).startsWith('potential_')).length;
    const potentialInOwned = ownedBadges.filter(badge => String(badge.id).startsWith('potential_')).length;
    const potentialInAvailable = availableBadges.filter(badge => String(badge.id).startsWith('potential_')).length;
    
    console.log('\n📝 潜在可获得勋章(potential_)分析:');
    console.log(`全部勋章中的潜在勋章: ${potentialInAll}个`);
    console.log(`已获得勋章中的潜在勋章: ${potentialInOwned}个`);
    console.log(`可获得勋章中的潜在勋章: ${potentialInAvailable}个`);
    
    // 6. 建议修复方案
    console.log('\n🔧 修复建议:');
    let hasIssues = false;
    
    if (allBadges.length !== expectedTotal) {
      console.log('1. 修复全部勋章的数量与已获得+可获得勋章数量不一致的问题');
      console.log(`   - 当前: ${allBadges.length}, 预期: ${expectedTotal}`);
      hasIssues = true;
    }
    
    if (missingInAll.length > 0) {
      console.log('2. 确保全部勋章中包含所有已获得和可获得的勋章');
      hasIssues = true;
    }
    
    if (overlapCount > 0) {
      console.log('3. 确保已获得勋章和可获得勋章之间没有重叠');
      hasIssues = true;
    }
    
    if (potentialInOwned > 0) {
      console.log('4. 移除已获得勋章中的潜在勋章(potential_)');
      hasIssues = true;
    }
    
    // 7. 总结
    console.log('\n📝 最终结论:');
    if (!hasIssues) {
      console.log('✅ 勋章显示功能已修复成功');
    } else {
      console.log('❌ 勋章显示功能仍有问题需要修复');
    }
    
  } catch (error) {
    console.error('❌ 测试过程出错:', error.message);
    if (error.response) {
      console.error('详细错误:', error.response.data);
    }
  }
})(); 